package com.apes.fn.scm.used.model;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.*;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.car.model.EmissionStandard;
import com.apes.scm.masterdata.finance.model.BankAccount;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.party.service.PartyRoleService;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 功能：二手车
 *
 * @author Linwz
 * @create 2020-08-11 15:15:40
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = "war_used_car")
public class UsedCar extends Model {
    public static final String CONST_TABLE_NAME = "war_used_car";

    @Id
    @Column(name = "id", length = 16)
    @Comment("二手车订单")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "二手车订单", prefix = "E%(yy)%(month)", padding = 5, useDateRange=true)
    private String id;  //编码

    @Comment("接车门店")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    private Dept dept;

    @Comment("跟进人")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "follow_id", referencedColumnName = "id")
    private PartyRole followId; // 跟进人

    @Column(name = "follow_Commission", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("跟进人提成")
    private double followCommission = 0;

    @Comment("检测师")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "inspector_id", referencedColumnName = "id")
    private PartyRole inspectorId; // 检测师

    @Column(name = "inspector_Commission", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("检测师提成")
    private double inspectorCommission = 0;

    @Comment("店长")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "store_Manager_id", referencedColumnName = "id")
    private PartyRole storeManagerId; // 店长

    @Column(name = "store_Manager_Commission", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("店长提成")
    private double storeManager = 0;

    @Comment("总部人员")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "headquarters_id", referencedColumnName = "id")
    private PartyRole headquartersId; // 总部人员

    @Column(name = "headquarters_Commission", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总部人员提成")
    private double headquartersCommission = 0;

    @Comment("车牌")
    @Column(name = "license_Plate")
    private  String licensePlate;

    @Comment("注册日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "registration_date")
    private Date registrationDate;

    @Column(name = "car_brand",length = 60,nullable = false)
    @Comment("汽车品牌")
    private String carBrand;//汽车品牌

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "CHX_ID", referencedColumnName = "id")
    @Comment("车型")
    private CarModel carModel;

//    //车型
    @JoinColumn(name = "jekun_car_model", referencedColumnName = "id",nullable = false)
    @Comment("车型")
    private String jekunCarModel;

    public String getJekunCarModel(){
        if (this.carModel != null)
            return this.carModel.getName();
        else
            return this.jekunCarModel;
    }


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "car_configuration_id",referencedColumnName = "id")
    private CarConfiguration carModelLabel; // 配置车型编码  从车辆主数据取

    @Column(name = "jekun_model_code", length = 64)
    @Comment("配置车型编码")
    private String jekunModelCode;

    @Column(name = "car_model_label_name", length = 256)
    @Comment("配置车型名称")
    private String carModelLabelName;

    public String getCarModelLabelName(){
        if (carModelLabel != null) {
            return carModelLabel.getName();
        } else
            return carModelLabelName;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PFBZID", referencedColumnName = "id")
    @Comment("排放标准")
    private EmissionStandard emissionStandard;

    @Column(name = "frame_number", length = 64)
    @Comment("车架号码")
    private String frameNumber; //车架号码

    @Column(name = "engine_number", length = 64)
    @Comment("发动机号码")
    private String engineNumber; //发动机号码

    @Column(name = "car_number", length = 64)
    @Comment("车辆编码")
    private String carNumber; //车辆编码

    @Column(name = "final_Price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("成交价")
    private double finalPrice = 0;

    @Column(name = "store_Price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("门店服务费")
    private double storePrice = 0;

    @Column(name = "headquarters_Price", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("总部服务费")
    private double headquartersPrice = 0;

    @Comment("成交日期")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "final_date")
    private Date finalDate;

    @Column(name = "final_method", length = 64)
    @Comment("成交方式")
    private String finalMethod; //成交方式

    @Comment("合同过户期限")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "transfer_date")
    private Date transferDate;

    @Column(name = "car_owner", length = 64)
    @Comment("车主")
    private String carOwner; //车主

    @Column(name = "telephone", length = 64)
    @Comment("联系电话")
    private String telephone; //联系电话

    @Column(name = "car_owner_deposit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("车主押金")
    private double carOwnerDeposit = 0;

    @Column(name = "bank", length = 32)
    @Comment("开户行")
    private String bank; //开户行

    @Column(name = "bank_account", length = 32)
    @Comment("开户行帐号")
    private String bankAccount; //开户行帐号

    @Column(name = "bank_name", length = 32)
    @Comment("开户名")
    private String bankName; //开户名

    @Column(name = "car_dealer", length = 32)
    @Comment("车商商号")
    private String carDealer;

    @Column(name = "dealer_telephone", length = 32)
    @Comment("车商联系电话")
    private String dealerTelephone; //车商联系电话

    @Column(name = "car_dealer_deposit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("车商押金")
    private double carDealerDeposit = 0;

    @Column(name = "dealer_person", length = 32)
    @Comment("联系人")
    private String dealerPerson;

    @Column(name = "dealer_bank", length = 32)
    @Comment("开户行")
    private String dealerBank; //开户行

    @Column(name = "dealer_bank_account", length = 32)
    @Comment("开户行帐号")
    private String dealerBankAccount; //开户行帐号

    @Column(name = "dealer_bank_name", length = 32)
    @Comment("开户名")
    private String dealerBankName; //开户名

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    @Column(name = "state", length = 10, nullable = false)
    private String state; //单据状态： ('create', '创建'), ('delete', '删除'),

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    @Comment("创建人")
    private PartyRole createUID; //审核

    @Comment("创建日期")
    @Column(name = "date_create")
    private Date dateCreate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUID; //审核

    @Comment("审批日期")
    @Column(name = "date_approve")
    private Date dateApprove;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司")
    private PartyRole company;

    @Column(name = "calculate_flag", columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("计费完成标记")
    private boolean calculateFlag = false;

    /***
     * 取 [B05]二手车交易管理费
     */
    @Column(name = "platform_management_fee", nullable = false, columnDefinition = ApesConst.FIELD_16_4_0)
    @Comment("平台管理费")
    private double platformManagementFee = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="bank_account_id", referencedColumnName="id")
    @Comment("结算账户")
    private BankAccount settleAccount;

    @Transient
    @Comment("结算帐号")
    private String settleAccountId;

    public String getSettleAccountId(){
        String code = null;
        if (settleAccount != null) {
            code = settleAccount.getAccountId();
        }
        return code;
    }


    /**
     * 平台公司
     **/
    @Transient
    private PartyRole platformCompany;


    public PartyRole getPlatformCompany() {
        if (platformCompany != null) return platformCompany;
        String topic = "event:fn.contract.findFirstValidFnContractCompanyByPartnerId";
        String platformCompanyId = (String) EventUtil.sendSynEvent(this, topic, MapUtil.mapper("partnerId", getCompany().getId())).stream().findFirst().get();
        if (!StringUtils.isEmpty(platformCompanyId)) {
            platformCompany = (PartyRole) JpaUtil.getImplementation(SpringManager.getBean(PartyRoleService.class).findOne(platformCompanyId, "Company"));
            platformCompany = MapUtil.getO(MapUtil.mapped(platformCompany), PartyRole.class);
        }
        return  platformCompany;
    }

    @Column(name = "account_bank", length = 64)
    @Comment("收款账户")
    private String accountBank;

    @Column(name = "account_number", length = 64)
    @Comment("收款账号")
    private String accountNumber;

}
